<script setup lang="ts">
import { ref, reactive, computed } from 'vue'

// 定义任务接口
interface TodoItem {
  id: number;
  content: string;
  completed: boolean;
  category: string;
  createdAt: Date;
}

// 定义分类选项
const categories = ['工作', '学习', '生活', '健康', '其他']

// 定义输入文本变量
const inputText = ref('')
const selectedCategory = ref('其他')

// 定义任务列表
const todos = reactive<TodoItem[]>([
  { id: 1, content: '买菜 买豆腐', completed: false, category: '生活', createdAt: new Date() }
])

// 定义已归档任务列表
const archivedTodos = reactive<TodoItem[]>([])

// 获取下一个ID
const getNextId = () => {
  return Math.max(...todos.map(todo => todo.id), ...archivedTodos.map(todo => todo.id), 0) + 1
}

// 添加任务
const addTodo = () => {
  if (inputText.value.trim()) {
    todos.push({
      id: getNextId(),
      content: inputText.value.trim(),
      completed: false,
      category: selectedCategory.value,
      createdAt: new Date()
    })
    inputText.value = ''
    // 添加成功的提示动画
    showSuccessMessage()
  }
}

// 清空输入框
const clearInput = () => {
  inputText.value = ''
  selectedCategory.value = '其他'
}

// 切换任务完成状态
const toggleTodoStatus = (id: number) => {
  const todo = todos.find(todo => todo.id === id)
  if (todo) {
    todo.completed = !todo.completed
  }
}

// 删除任务
const deleteTodo = (id: number) => {
  const index = todos.findIndex(todo => todo.id === id)
  if (index !== -1) {
    todos.splice(index, 1)
  }
}

// 归档已完成任务
const archiveCompleted = () => {
  const completedTodos = todos.filter(todo => todo.completed)
  archivedTodos.push(...completedTodos)
  
  // 从当前任务列表中移除已归档的任务
  completedTodos.forEach(todo => {
    deleteTodo(todo.id)
  })
  
  // 添加归档成功的提示动画
  showArchiveMessage()
}

// 按Enter键添加任务
const handleKeyPress = (event: KeyboardEvent) => {
  if (event.key === 'Enter') {
    addTodo()
  }
}

// 计算已完成任务百分比
const completionRate = computed(() => {
  if (todos.length === 0) return 0
  return Math.round((todos.filter(t => t.completed).length / todos.length) * 100)
})

// 成功消息动画
const showSuccessMessage = () => {
  const message = document.createElement('div')
  message.className = 'success-message'
  message.textContent = '任务添加成功！'
  document.body.appendChild(message)
  
  setTimeout(() => {
    message.classList.add('fade-out')
    setTimeout(() => {
      document.body.removeChild(message)
    }, 300)
  }, 2000)
}

// 归档消息动画
const showArchiveMessage = () => {
  const message = document.createElement('div')
  message.className = 'archive-message'
  message.textContent = '已成功归档完成的任务！'
  document.body.appendChild(message)
  
  setTimeout(() => {
    message.classList.add('fade-out')
    setTimeout(() => {
      document.body.removeChild(message)
    }, 300)
  }, 2000)
}

// 获取分类对应的颜色
const getCategoryColor = (category: string) => {
  const colorMap: Record<string, string> = {
    '工作': '#4f46e5',
    '学习': '#10b981',
    '生活': '#f59e0b',
    '健康': '#ef4444',
    '其他': '#8b5cf6'
  }
  return colorMap[category] || '#8b5cf6'
}
</script>

<template>
  <div class="app-container">
    <!-- 装饰元素 -->
    <div class="decorative-circle circle-1"></div>
    <div class="decorative-circle circle-2"></div>
    <div class="decorative-circle circle-3"></div>
    
    <div class="todo-app">
      <!-- 应用头部 -->
      <div class="app-header">
        <h1 class="app-title">
          <span class="title-icon">📝</span>
          TODO-List 任务清单
        </h1>
        
        <!-- 任务统计 -->
        <div class="stats-container">
          <div class="stat-item">
            <span class="stat-number">{{ todos.length }}</span>
            <span class="stat-label">总任务</span>
          </div>
          <div class="stat-item">
            <span class="stat-number">{{ todos.filter(t => t.completed).length }}</span>
            <span class="stat-label">已完成</span>
          </div>
          <div class="stat-item">
            <span class="stat-number">{{ completionRate }}%</span>
            <span class="stat-label">完成率</span>
          </div>
        </div>
      </div>
      
      <!-- 输入区域 -->
      <div class="input-section">
        <label for="todo-input" class="input-label">添加新任务</label>
        <div class="input-row">
          <input 
            id="todo-input"
            type="text" 
            v-model="inputText" 
            placeholder="输入任务内容..."
            @keyup.enter="addTodo"
            class="todo-input"
          >
          
          <!-- 分类选择 -->
          <select 
            v-model="selectedCategory" 
            class="category-select"
          >
            <option v-for="category in categories" :key="category" :value="category">
              {{ category }}
            </option>
          </select>
        </div>
        
        <div class="button-group">
          <button @click="addTodo" class="btn btn-primary" :disabled="!inputText.trim()">
            <span class="btn-icon">✚</span> 添加任务
          </button>
          <button @click="clearInput" class="btn btn-secondary">
            <span class="btn-icon">🗑️</span> 清空
          </button>
        </div>
      </div>
      
      <!-- 进度条 -->
      <div class="progress-section">
        <div class="progress-label">完成进度</div>
        <div class="progress-bar-container">
          <div class="progress-bar" :style="{ width: completionRate + '%' }"></div>
        </div>
        <div class="progress-text">{{ completionRate }}%</div>
      </div>
      
      <!-- 任务列表 -->
      <div class="list-section">
        <h2 class="section-title">
          <span class="title-icon">📋</span>
          任务列表
        </h2>
        <div class="todo-list">
          <div v-if="todos.length === 0" class="empty-message">
            <div class="empty-icon">📝</div>
            <div class="empty-text">暂无任务</div>
            <div class="empty-subtext">点击上方添加按钮创建第一个任务吧！</div>
          </div>
          <transition-group name="task" tag="div">
            <div v-for="todo in todos" :key="todo.id" class="task-item">
              <div class="task-left">
                <label class="task-checkbox-container">
                  <input 
                    type="checkbox" 
                    v-model="todo.completed" 
                    class="task-checkbox"
                  >
                  <span class="checkbox-check"></span>
                </label>
                <div class="task-details">
                  <span :class="{ 'completed': todo.completed }" class="task-content">
                    {{ todo.content }}
                  </span>
                  <div class="task-meta">
                    <span class="task-category" :style="{ backgroundColor: getCategoryColor(todo.category) }">
                      {{ todo.category }}
                    </span>
                    <span class="task-time">
                      {{ new Date(todo.createdAt).toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' }) }}
                    </span>
                  </div>
                </div>
              </div>
              <button @click="deleteTodo(todo.id)" class="delete-btn" title="删除任务">
                <span class="delete-icon">×</span>
              </button>
            </div>
          </transition-group>
        </div>
      </div>
      
      <!-- 归档按钮 -->
      <div class="archive-section">
        <button 
          @click="archiveCompleted" 
          class="btn btn-archive"
          :disabled="todos.every(todo => !todo.completed)"
          title="归档已完成任务"
        >
          <span class="btn-icon">📁</span> 归档已完成
        </button>
      </div>
    </div>
    
    <!-- 归档信息 -->
    <div v-if="archivedTodos.length > 0" class="archive-info">
      <span class="archive-icon">📁</span>
      已归档 {{ archivedTodos.length }} 个任务
    </div>
  </div>
</template>

<style scoped>
/* 全局样式重置 */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

/* 主容器样式 */
.app-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
  position: relative;
  overflow: hidden;
}

/* 装饰元素 */
.decorative-circle {
  position: absolute;
  border-radius: 50%;
  opacity: 0.5;
  filter: blur(60px);
  animation: float 8s ease-in-out infinite;
}

.circle-1 {
  width: 300px;
  height: 300px;
  background: #4f46e5;
  top: -100px;
  left: -100px;
  animation-delay: 0s;
}

.circle-2 {
  width: 200px;
  height: 200px;
  background: #ec4899;
  bottom: -50px;
  right: -50px;
  animation-delay: 2s;
}

.circle-3 {
  width: 150px;
  height: 150px;
  background: #10b981;
  top: 50%;
  right: 10%;
  animation-delay: 4s;
}

/* 浮动动画 */
@keyframes float {
  0%, 100% {
    transform: translate(0, 0);
  }
  50% {
    transform: translate(20px, 20px);
  }
}

/* TODO应用主体样式 */
.todo-app {
  width: 100%;
  max-width: 600px;
  background: white;
  border-radius: 24px;
  padding: 32px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  position: relative;
  z-index: 1;
}

.todo-app:hover {
  transform: translateY(-8px);
  box-shadow: 0 25px 75px rgba(0, 0, 0, 0.2);
}

/* 应用头部 */
.app-header {
  text-align: center;
  margin-bottom: 32px;
}

/* 标题样式 */
.app-title {
  color: #2d3748;
  margin-bottom: 16px;
  font-size: 32px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
}

.title-icon {
  font-size: 36px;
  animation: bounce 2s infinite;
}

@keyframes bounce {
  0%, 20%, 50%, 80%, 100% {
    transform: translateY(0);
  }
  40% {
    transform: translateY(-10px);
  }
  60% {
    transform: translateY(-5px);
  }
}

/* 统计信息 */
.stats-container {
  display: flex;
  justify-content: center;
  gap: 32px;
  margin-top: 24px;
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.stat-number {
  font-size: 28px;
  font-weight: 700;
  color: #667eea;
}

.stat-label {
  font-size: 12px;
  color: #718096;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

/* 输入区域样式 */
.input-section {
  margin-bottom: 32px;
}

.input-label {
  display: block;
  color: #4a5568;
  font-weight: 600;
  font-size: 16px;
  margin-bottom: 12px;
}

.input-row {
  display: flex;
  gap: 12px;
  margin-bottom: 16px;
}

.todo-input {
  flex: 1;
  padding: 14px 20px;
  border: 2px solid #e2e8f0;
  border-radius: 12px;
  font-size: 16px;
  transition: all 0.3s ease;
  background-color: #fafafa;
}

.todo-input:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.1);
  background-color: white;
}

/* 分类选择器 */
.category-select {
  padding: 14px 20px;
  border: 2px solid #e2e8f0;
  border-radius: 12px;
  font-size: 14px;
  background-color: white;
  cursor: pointer;
  transition: all 0.3s ease;
  min-width: 120px;
}

.category-select:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.1);
}

/* 按钮组样式 */
.button-group {
  display: flex;
  gap: 12px;
}

/* 按钮基础样式 */
.btn {
  padding: 14px 24px;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  position: relative;
  overflow: hidden;
}

.btn::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 0;
  height: 0;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.3);
  transform: translate(-50%, -50%);
  transition: width 0.6s, height 0.6s;
}

.btn:hover::before {
  width: 300px;
  height: 300px;
}

/* 主按钮样式 */
.btn-primary {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  box-shadow: 0 4px 16px rgba(102, 126, 234, 0.3);
}

.btn-primary:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(102, 126, 234, 0.4);
}

/* 次要按钮样式 */
.btn-secondary {
  background-color: #e2e8f0;
  color: #4a5568;
}

.btn-secondary:hover {
  background-color: #cbd5e0;
  transform: translateY(-1px);
}

/* 归档按钮样式 */
.btn-archive {
  background: linear-gradient(135deg, #4fd1c5 0%, #38b2ac 100%);
  color: white;
  margin-top: 24px;
  width: 100%;
  box-shadow: 0 4px 16px rgba(79, 209, 197, 0.2);
}

.btn-archive:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(79, 209, 197, 0.3);
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
}

.btn-icon {
  font-size: 18px;
}

/* 进度条 */
.progress-section {
  margin-bottom: 32px;
  background: #f7fafc;
  padding: 20px;
  border-radius: 16px;
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.05);
}

.progress-label {
  font-size: 14px;
  color: #4a5568;
  font-weight: 500;
  margin-bottom: 12px;
}

.progress-bar-container {
  height: 12px;
  background-color: #e2e8f0;
  border-radius: 6px;
  overflow: hidden;
  position: relative;
}

.progress-bar {
  height: 100%;
  background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
  border-radius: 6px;
  transition: width 0.6s ease;
  position: relative;
}

.progress-bar::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: linear-gradient(
    45deg,
    rgba(255, 255, 255, 0.2) 25%,
    transparent 25%,
    transparent 50%,
    rgba(255, 255, 255, 0.2) 50%,
    rgba(255, 255, 255, 0.2) 75%,
    transparent 75%,
    transparent
  );
  background-size: 20px 20px;
  animation: progress-animation 1s linear infinite;
}

@keyframes progress-animation {
  0% {
    background-position: 0 0;
  }
  100% {
    background-position: 20px 0;
  }
}

.progress-text {
  text-align: center;
  margin-top: 8px;
  font-size: 16px;
  font-weight: 600;
  color: #667eea;
}

/* 列表区域样式 */
.list-section {
  margin-bottom: 24px;
}

.section-title {
  color: #4a5568;
  font-weight: 600;
  font-size: 20px;
  margin-bottom: 16px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.todo-list {
  background-color: #f7fafc;
  border-radius: 16px;
  padding: 20px;
  min-height: 200px;
  max-height: 400px;
  overflow-y: auto;
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.05);
}

/* 空任务消息样式 */
.empty-message {
  text-align: center;
  padding: 60px 20px;
  color: #a0aec0;
}

.empty-icon {
  font-size: 64px;
  margin-bottom: 16px;
  opacity: 0.5;
}

.empty-text {
  font-size: 18px;
  font-weight: 500;
  margin-bottom: 8px;
}

.empty-subtext {
  font-size: 14px;
  opacity: 0.8;
}

/* 任务项样式 */
.task-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  background-color: white;
  border-radius: 12px;
  margin-bottom: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  border: 2px solid transparent;
}

.task-item:hover {
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
  border-color: #e2e8f0;
  transform: translateX(4px);
}

.task-item:last-child {
  margin-bottom: 0;
}

.task-left {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  flex: 1;
}

/* 自定义复选框 */
.task-checkbox-container {
  position: relative;
  padding-left: 32px;
  cursor: pointer;
  display: flex;
  align-items: center;
}

.task-checkbox {
  position: absolute;
  opacity: 0;
  cursor: pointer;
  height: 0;
  width: 0;
}

.checkbox-check {
  position: absolute;
  top: 0;
  left: 0;
  height: 24px;
  width: 24px;
  background-color: #f3f4f6;
  border-radius: 6px;
  border: 2px solid #e2e8f0;
  transition: all 0.3s ease;
}

.task-checkbox-container:hover .checkbox-check {
  background-color: #e5e7eb;
  border-color: #d1d5db;
}

.task-checkbox:checked ~ .checkbox-check {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-color: #667eea;
}

.checkbox-check:after {
  content: "";
  position: absolute;
  display: none;
  left: 8px;
  top: 4px;
  width: 6px;
  height: 12px;
  border: solid white;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}

.task-checkbox:checked ~ .checkbox-check:after {
  display: block;
}

/* 任务详情 */
.task-details {
  flex: 1;
}

/* 任务内容样式 */
.task-content {
  display: block;
  color: #2d3748;
  font-size: 16px;
  font-weight: 500;
  transition: all 0.3s ease;
  word-break: break-word;
  margin-bottom: 8px;
}

.task-content.completed {
  color: #a0aec0;
  text-decoration: line-through;
  font-weight: 400;
}

/* 任务元数据 */
.task-meta {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* 任务分类标签 */
.task-category {
  padding: 4px 12px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
  color: white;
  background-color: #8b5cf6;
}

/* 任务时间 */
.task-time {
  font-size: 12px;
  color: #a0aec0;
}

/* 删除按钮样式 */
.delete-btn {
  background: none;
  border: none;
  color: #e53e3e;
  font-size: 24px;
  cursor: pointer;
  padding: 4px 12px;
  border-radius: 8px;
  transition: all 0.3s ease;
  line-height: 1;
  opacity: 0;
}

.task-item:hover .delete-btn {
  opacity: 1;
}

.delete-btn:hover {
  background-color: #fed7d7;
  transform: scale(1.1);
}

.delete-icon {
  display: block;
}

/* 归档信息样式 */
.archive-info {
  margin-top: 24px;
  color: white;
  font-size: 14px;
  background-color: rgba(255, 255, 255, 0.2);
  padding: 12px 20px;
  border-radius: 20px;
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  gap: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.archive-info:hover {
  background-color: rgba(255, 255, 255, 0.3);
  transform: translateY(-2px);
}

.archive-icon {
  font-size: 16px;
}

/* 消息提示样式 */
.success-message,
.archive-message {
  position: fixed;
  top: 20px;
  right: 20px;
  padding: 16px 24px;
  border-radius: 12px;
  color: white;
  font-weight: 600;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  z-index: 1000;
  animation: slideInRight 0.3s ease-out;
}

.success-message {
  background: linear-gradient(135deg, #10b981 0%, #059669 100%);
}

.archive-message {
  background: linear-gradient(135deg, #4f46e5 0%, #4338ca 100%);
}

.success-message.fade-out,
.archive-message.fade-out {
  animation: fadeOut 0.3s ease-in;
  opacity: 0;
}

@keyframes slideInRight {
  from {
    transform: translateX(100%);
    opacity: 0;
  }
  to {
    transform: translateX(0);
    opacity: 1;
  }
}

@keyframes fadeOut {
  from {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
}

/* 任务动画 */
.task-enter-active,
.task-leave-active {
  transition: all 0.5s ease;
}

.task-enter-from {
  opacity: 0;
  transform: translateX(-30px) scale(0.9);
}

.task-leave-to {
  opacity: 0;
  transform: translateX(30px) scale(0.9);
}

.task-move {
  transition: transform 0.5s ease;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .todo-app {
    padding: 24px;
  }
  
  .app-title {
    font-size: 28px;
  }
  
  .stats-container {
    gap: 20px;
  }
  
  .input-row {
    flex-direction: column;
  }
  
  .category-select {
    min-width: auto;
  }
  
  .button-group {
    flex-direction: column;
  }
  
  .task-item {
    padding: 14px 16px;
  }
  
  .task-content {
    font-size: 15px;
  }
  
  .decorative-circle {
    transform: scale(0.7);
  }
}

@media (max-width: 480px) {
  .todo-app {
    padding: 20px;
  }
  
  .app-title {
    font-size: 24px;
  }
  
  .stats-container {
    gap: 16px;
  }
  
  .stat-number {
    font-size: 24px;
  }
  
  .todo-input,
  .btn {
    padding: 12px 16px;
    font-size: 14px;
  }
  
  .task-item {
    padding: 12px 14px;
  }
  
  .task-content {
    font-size: 14px;
  }
  
  .empty-icon {
    font-size: 48px;
  }
  
  .empty-text {
    font-size: 16px;
  }
}
</style>